import { Inject, Injectable } from '../decorators/inject';
import { Hook } from './hook';

type TCancelSyncRender = {cancel: () => void};

@Injectable()
export class SyncRenderManage {
    protected store: Map<string, TCancelSyncRender> = new Map();
    constructor(
        protected hook: Hook
    ) {
        this.hook.beforeRender.add(() => {
            this.store.forEach(item => {
                item.cancel();
            });
            this.store.clear();
        });
    }
    public init() {
        // this.hook.beforeRender.add(() => {
        //     console.log('--beforerender---')
        //     this.store.forEach(item => {
        //         item.cancel();
        //     })
        // });
        // this.store.clear();
    }
    public add(row: number, col: number, cancel: TCancelSyncRender) {
        let key = `${row}_${col}`;
        // this.cancel(row, col);
        this.store.set(key, cancel);
    }
    public cancel(row: number, col: number) {
        let key = `${row}_${col}`;
        if(this.store.has(key)) {
            let cancelInstance = this.store.get(key) as TCancelSyncRender;
            cancelInstance.cancel();
        }
    }
}